package com.vgemv.jsutilitysdk.Animation;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.Size;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.vgemv.jsutilitysdk.Display.JsDisplayManagerGamma.JsDisplayManagerGamma;
import com.vgemv.jsutilitysdk.Display.JsDisplayManagerGamma.JsDisplayStyleGamma;
import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.R;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class JsHeartFlowAnimationManager {

    static private Random mRandom = new Random();
    static private int aniamtinTime = 3000;
    static private Map<View,JsHeartFlowAnimationStyle> map_style = new HashMap();
    static private int[] likeImages = {R.mipmap.like1_blue,R.mipmap.like1_green,R.mipmap.like1_orange,R.mipmap.like1_red,R.mipmap.like1_violet,R.mipmap.like1_yellow};

    /**
     * 以指定位置为中心,创建点赞图片,上升一段距离
     * @param fromCenterPointInScreen
     * @param imageSize
     * @param activity
     */
    static public void startAnimation(final Point fromCenterPointInScreen, Size imageSize, Activity activity) {
        final ImageView iv = new ImageView(activity);
        int i = mRandom.nextInt(likeImages.length);
        iv.setImageResource(likeImages[i]);

        //fromView在屏幕中的中心点-contentView的top才是相对于contentView的位置.
        int centerX = fromCenterPointInScreen.x;
        int centerY = fromCenterPointInScreen.y - JsGeometryManager.contentFrameLayoutTopInScreen(activity);
        //状态栏是否透明会影响位置
        //不透明时      R.id.content是从状态栏下方开始
        //透明时       R.id.content从屏幕0开始


        JsDisplayStyleGamma style = new JsDisplayStyleGamma();
        style.mb_isShowMaskView = false;

        int viewWidth = imageSize.getWidth();
        int viewHeight = imageSize.getHeight();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(imageSize.getWidth(),imageSize.getHeight());
        lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        int x = (int)centerX - viewWidth/2;
        int y= (int)centerY - viewHeight/2;

        lp.topMargin = y;
        lp.leftMargin = x;
        JsDisplayManagerGamma.display(activity,iv,style,lp,null);

        JsHeartFlowAnimationStyle animationStyle = new JsHeartFlowAnimationStyle();
        animationStyle.mPt_startPositon = new PointF(x,y);
        animationStyle.mS_viewSize = imageSize;
        map_style.put(iv,animationStyle);

        AnimatorSet togetherAnimator = getAllAnimator(iv);

        togetherAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
//                JsLogManager.logc_d("JsHeartFlowAnimationManager","onAnimationStart");
            }
            @Override
            public void onAnimationEnd(Animator animation) {
//                JsLogManager.logc_d("JsHeartFlowAnimationManager","onAnimationEnd");
                // 动画结束，移除小心形
                JsDisplayManagerGamma.undisplay(iv);
            }
            @Override
            public void onAnimationCancel(Animator animation) {
            }
            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        try {
            togetherAnimator.start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    static private ObjectAnimator getAlphaAnimator(View loveImage) {
        return ObjectAnimator.ofFloat(loveImage, "alpha", (float) 1f, 0.1f).setDuration(aniamtinTime);
    }

    static private ObjectAnimator getScaleAnimatorX(View loveImage) {
        return ObjectAnimator.ofFloat(loveImage, "scaleX", (float)  1,0.1f).setDuration(aniamtinTime);
    }

    static private ObjectAnimator getScaleAnimatorY(View loveImage) {
        return ObjectAnimator.ofFloat(loveImage, "scaleY", (float)  1,0.1f).setDuration(aniamtinTime);
    }

    static private ObjectAnimator getTranslationObjectX(View loveImage) {
        return ObjectAnimator.ofFloat(loveImage, "translationX", 0, 18).setDuration(aniamtinTime);
    }

    static private ObjectAnimator getTranslationObjectY(View loveImage) {
        return ObjectAnimator.ofFloat(loveImage, "translationY", 0, -888).setDuration(aniamtinTime);
    }

    static private AnimatorSet getAnimatorSet(View view) {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(getAlphaAnimator(view), getScaleAnimatorX(view),
                getScaleAnimatorY(view));
        return animatorSet;
    }

    static private AnimatorSet getAllAnimator(View view) {
        AnimatorSet animatorSet = new AnimatorSet();
        //按照顺序进行
//        animatorSet.playSequentially(getBezierAnimator(view),getAnimatorSet(view));
        animatorSet.playTogether(getBezierAnimator(view),getAnimatorSet(view));
        return animatorSet;
    }



    /**
     * 使用自定义估值器生成贝塞尔曲线
     *
     * @param view
     * @return
     */
    static private ValueAnimator getBezierAnimator(final View view) {

        Size size = JsGeometryManager.getScreenSizeInPixel();
        int mWidth = size.getWidth();
        int mHeight = size.getHeight();
        JsHeartFlowAnimationStyle style = map_style.get(view);
        int viewWidth = style.mS_viewSize.getWidth();
        int viewHeight = style.mS_viewSize.getHeight();

        float x = style.mPt_startPositon.x;
        float y = style.mPt_startPositon.y;
        int flowDistance = 1000;
        // 求控制点
//        PointF p1 = new PointF(mRandom.nextInt(mWidth), mRandom.nextInt(mHeight / 2) + mHeight / 2);
//        PointF p2 = new PointF(mRandom.nextInt(mWidth), mRandom.nextInt(mHeight / 2));
        PointF p1 = new PointF(x +  mRandom.nextInt(200) - 150, y - mRandom.nextInt(flowDistance / 2));
        PointF p2 = new PointF(x +  mRandom.nextInt(200) - 150, y - flowDistance/2 - mRandom.nextInt(flowDistance/2));
        // 求起始点和终点
        PointF startPoint = style.mPt_startPositon;

        PointF endPoint = new PointF(startPoint.x + mRandom.nextInt(200) - 150, startPoint.y - flowDistance);

        //valueAnimator,负责时间上的调用
        //typeEvaluator,负责赋值.
        JsBezierEvaluator evaluator = new JsBezierEvaluator(p1, p2);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(evaluator,startPoint,endPoint);

        valueAnimator.setDuration(aniamtinTime);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                    // 自定义估值器BezierEvaluator的贝塞尔公式算出的 point
                    PointF bezierPoint = (PointF) animator.getAnimatedValue();
                    view.setX(bezierPoint.x);
                    view.setY(bezierPoint.y);
//                    view.setY(1000 * (1-animator.getAnimatedFraction()));
//                    view.setAlpha((float) (1 - animator.getAnimatedFraction() + 0.2));

            }
        });
        return valueAnimator;
    }
}

